﻿<UserControl x:Class="Gallery.MVVM.CommandingSample.CommandingSampleView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:apexMVVM="clr-namespace:Apex.MVVM;assembly=Apex"
        xmlns:apexCommands="clr-namespace:Apex.Commands;assembly=Apex"
        xmlns:apexControls="clr-namespace:Apex.Controls;assembly=Apex"
        xmlns:apexConverters="clr-namespace:Apex.Converters;assembly=Apex"
             xmlns:CommandingSample="clr-namespace:Gallery.MVVM.CommandingSample"
             xmlns:System="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="800">
    
    <UserControl.DataContext>
        <CommandingSample:CommandingSampleViewModel />
    </UserControl.DataContext>

    <Grid Style="{StaticResource GalleryItemStyle}">

        <apexControls:PaddedGrid Rows="Auto,Auto,*,Auto" Padding="4">

            <!-- Title section. -->
            <TextBlock Grid.Row="0" Style="{StaticResource Heading1}">COMMANDING SAMPLE</TextBlock>
            <TextBlock Grid.Row="1" Style="{StaticResource Subtitle}">
            This sample demonstrates how Commands can be used.
            </TextBlock>

            <!-- Sample zone. -->
            <Grid Row="2" Margin="16" MaxWidth="600" MaxHeight="800">

                <!-- The data entry grid. -->
                <apexControls:ApexGrid Columns="*,*">

                    <!-- Left hand column, the messages. -->
                    <apexControls:ApexGrid Grid.Column="1" Margin="4" Rows="Auto,*">

                        <TextBlock Grid.Row="0" Text="Output" FontSize="24" />
                        <ScrollViewer Grid.Row="1">
                            <ItemsControl ItemsSource="{Binding Messages}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding}" TextWrapping="Wrap" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>

                    </apexControls:ApexGrid>

                    <!-- Right hand column, the commands. -->
                    <apexControls:ApexGrid Margin="4" Rows="Auto,*">

                        <apexControls:ApexGrid.Resources>
                            <apexConverters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
                        </apexControls:ApexGrid.Resources>

                        <TextBlock Grid.Row="0" Text="Command" FontSize="24"  />

                        <ScrollViewer Grid.Row="1" >

                            <StackPanel Orientation="Vertical">

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="A simple command example." />
                                    <Button Width="160" Content="Simple Command" Command="{Binding SimpleCommand}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="A lamba command example." />
                                    <Button Width="160" Content="Lambda Command" Command="{Binding LambdaCommand}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="A parameterised command example." />
                                    <Button Width="160" Content="Parameterised Command" Command="{Binding ParameterisedCommand}" CommandParameter="Apples" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Enabling and disabling a command example." />
                                    <CheckBox IsChecked="{Binding EnableDisableCommand.CanExecute, Mode=TwoWay}" Content="Enabled" />
                                    <Button Width="160" Content="Enable/Disable Command" Command="{Binding EnableDisableCommand}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="A command with events." />
                                    <Button Width="160" Content="Events Command" Command="{Binding EventsCommand}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="An asynchronous command." />
                                    <Button Width="160" Content="Asynchronous Command" Command="{Binding AsyncCommand1}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Another asynchronous command." />
                                    <Button Width="160" Content="Asynchronous Command" Command="{Binding AsyncCommand2}" 
                                Visibility="{Binding AsyncCommand2.IsExecuting, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Invert}" />
                                    <StackPanel Visibility="{Binding AsyncCommand2.IsExecuting, Converter={StaticResource BooleanToVisibilityConverter}}">
                                        <TextBlock Margin="4" Text="The command is running!" />
                                        <ProgressBar Margin="4" Height="20" Width="120" IsIndeterminate="True" />
                                    </StackPanel>
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Cancellable Asynchronous Command." />
                                    <Button Width="160" Content="Cancellable Async Command" Command="{Binding CancellableAsyncCommand}" 
                                Visibility="{Binding CancellableAsyncCommand.IsExecuting, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Invert}" />
                                    <StackPanel Visibility="{Binding CancellableAsyncCommand.IsExecuting, Converter={StaticResource BooleanToVisibilityConverter}}">
                                        <TextBlock Margin="4" Text="The command is running!" />
                                        <ProgressBar Margin="4" Height="20" Width="120" IsIndeterminate="True" />
                                        <Button Margin="4" Width="100" Content="Cancel" Command="{Binding CancellableAsyncCommand.CancelCommand}" />
                                    </StackPanel>
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Disable During Execution Asynchronous Command." />
                                    <Button Width="160" Content="DDE Asynchronous Command" Command="{Binding DisabledDuringExecutionAsyncCommand}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Binding a command to an event example." />
                                    <Border Margin="20" Background="Red">
                                        <apexCommands:EventBindings.EventBindings>
                                            <apexCommands:EventBindingCollection>
                                                <apexCommands:EventBinding EventName="MouseLeftButtonDown" Command="{Binding EventBindingCommand}" />
                                            </apexCommands:EventBindingCollection>
                                        </apexCommands:EventBindings.EventBindings>
                                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Left Click on Me" FontSize="16" Foreground="White" />
                                    </Border>
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <StackPanel.Resources>
                                        <System:Int32 x:Key="IntValue">3</System:Int32>
                                    </StackPanel.Resources>
                                    <TextBlock TextWrapping="Wrap" Text="Typed Command (int)." />
                                    <Button Width="160" 
                                    Content="Int Typed Command" Command="{Binding IntTypedCommand}"
                                    CommandParameter="{StaticResource IntValue}" />
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Typed Command (string)." />
                                    <Button Width="160" 
                                    Content="String Typed Command" Command="{Binding StringTypedCommand}"
                                    CommandParameter="Some String Value"/>
                                </StackPanel>

                                <StackPanel Margin="10">
                                    <TextBlock TextWrapping="Wrap" Text="Invalid Typed Command (expecting int, receiving string)." />
                                    <Button Width="160" 
                                    Content="String Typed Command" Command="{Binding IntTypedCommand}"
                                    CommandParameter="Some String Value"/>
                                </StackPanel>
                            </StackPanel>
                        </ScrollViewer>

                    </apexControls:ApexGrid>
                </apexControls:ApexGrid>
            </Grid>
        </apexControls:PaddedGrid>
    </Grid>
</UserControl>